package com.julang.check.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.julang.check.bean.RequestEntity;
import com.julang.check.api.entity.YyHtmlReport;
import com.julang.check.api.service.impl.YyHtmlReportServiceImpl;
import com.julang.check.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.julang.check.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

import static com.julang.check.utils.CommonData.*;
import static com.julang.check.utils.MessageInfo.FILENOTEXIST;

/**
 * <p>
 * yy的检测文件相关操作
 * </p>
 *
 * @author 赵自强
 * @since 2019-06-05
 */
@RestController
@RequestMapping("/api/yy-html-report")
@Api(tags = "yy的检测文件相关操作")
public class YyHtmlReportController extends BaseController {
    @Autowired
    private YyHtmlReportServiceImpl htmlReportService;


    @ApiOperation("创建paperYY文件记录")
    @ApiImplicitParam(name = "Authorization", value = "jwt token", required = true, paramType = "header")
    @RequestMapping(value = "/add_file", method = {RequestMethod.POST})
    @ResponseBody
    public Result<String> addReportFile(@RequestBody RequestEntity.FileName fileName) {
        //获取文档记录数据
        YyHtmlReport result = htmlReportService.getHtmlFileData(this.getIntergerUserId(), fileName.getFileName());
        if (result == null) {
            return Result.buildError(CommonData.FAIL, FILENOTEXIST, "");
        }
        //判断文件是否存在
        if (htmlReportService.isRecordExists(result.getWordName(), result.getRepeatPercent())) {
            return Result.buildError(CommonData.FAIL, MessageInfo.FILEHAVEEXIST, "");
        }
        //保存到记录
        if (htmlReportService.save(result)) {
            return Result.buildSuccess(MessageInfo.UPLOADFILESUCCESSINFO, "");
        } else {
            return Result.buildError(CommonData.FAIL, MessageInfo.MYSQLERROR, "");
        }
    }

    @ApiOperation("根据用户Id获取用户上传的PaperYy解析记录")
    @ApiImplicitParam(name = "Authorization", value = "jwt token", required = true, paramType = "header")
    @RequestMapping(value = "/all_file", method = {RequestMethod.GET})
    @ResponseBody
    public Result<List<YyHtmlReport>> getAllHtmlFile() {
        List<YyHtmlReport> re = htmlReportService.list(new QueryWrapper<YyHtmlReport>()
                .lambda().eq(YyHtmlReport::getUserId, this.getUserId()));
        System.out.println("查到记录：" + re.size() + "  条");
        return Result.buildSuccess("", re);
    }

    @ApiOperation("根据PaperYy记录的Id获取记录的详情")
    @ApiImplicitParam(name = "Authorization", value = "jwt token", required = true, paramType = "header")
    @RequestMapping(value = "/record", method = {RequestMethod.GET})
    @ResponseBody
    public Result<YyHtmlReport> getOneRecord(@RequestParam("id")String id) {
        YyHtmlReport report = htmlReportService.getById(id);
        if (report == null) {
            return Result.buildError(CommonData.FAIL,MessageInfo.HTMLREPORTRECORDNOEXIST, null);
        }
        return Result.buildSuccess(MessageInfo.SUCCESSINFO, report);
    }


    @ApiOperation("上传PaperYy的Word文件")
    @ApiImplicitParam(name = "Authorization", value = "jwt token", required = true, paramType = "header")
    @RequestMapping(value = "/add_word", method = {RequestMethod.POST})
    @ResponseBody
    public Result<String> upload(MultipartFile file, @RequestParam String id) throws Exception {
        long s = System.currentTimeMillis();
        System.out.println("id:" + id);
        String originalFilename = file.getOriginalFilename();
        if (!originalFilename.contains(POINT)) {
            return Result.buildError(CommonData.FAIL, MessageInfo.FILEERRORINFO, null);
        }
        System.out.println("收到文件名字： " + file.getOriginalFilename());
        String[] datas = originalFilename.split("\\.");
        if (!datas[datas.length - 1].equals(DOCXSUFFIX)) {
            return Result.buildError(CommonData.FAIL, MessageInfo.FILEERRORINFO, null);
        }
        String suffix = "." + datas[datas.length - 1];
        File localFile = new File(SingnalData.filePath, s + suffix);
        file.transferTo(localFile);
        YyHtmlReport report = htmlReportService.getById(id);
        report.setDocPath(localFile.getAbsolutePath());
        System.out.println(report.toString());
        htmlReportService.updateById(report);
        System.out.println("上传文件耗时：" + (System.currentTimeMillis() - s) + "  毫秒");
        return Result.buildSuccess(MessageInfo.UPLOADFILESUCCESSINFO, localFile.getAbsolutePath());
    }
}
